//
//  JZDefine.swift
//  Example
//
//  Created by S JZ on 2024/1/30.
//

import Foundation


import UIKit

// OC 与 Swift全部可以使用
public class JZDefine: NSObject {
    // 获取 windowScene
    @available(iOS 13.0, *)
    @objc public static var windowScene: UIWindowScene? { UIApplication.shared.connectedScenes.first as? UIWindowScene }
    // MARK: 获取KeyWindow
    @objc public static var keyWindow: UIWindow? {
        if #available(iOS 13.0, *) {
            return windowScene?.windows.first { $0.isKeyWindow }
        }
        return UIApplication.shared.keyWindow
    }
    // 获取顶部 topWindow
    @objc public static var topWindow: UIWindow? {
        if #available(iOS 13.0, *) {
            return windowScene?.windows.last
        }
        return UIApplication.shared.windows.last
    }
    
    // MARK: - 获取屏幕宽高
    @objc public static var screenSize: CGSize {
        if #available(iOS 13.0, *) {
            return windowScene?.screen.bounds.size ?? .zero
        }
        return UIScreen.main.bounds.size
    }
    // 屏幕宽度
    @objc public static var screenWidth: CGFloat { screenSize.width }
    // 屏幕高度
    @objc public static var screenHeight: CGFloat { screenSize.height }

    // 用于判断是否为刘海屏
    @objc public static var isPhoneX: Bool { statusBarHeight > 30 }

    // MARK: - 屏幕宽高比例
    // 设置默认宽度为375.0
    @objc public static let defaultWidth: CGFloat = 375.0
    // 当前屏幕 与 默认宽度 比利
    @objc public static var fit: CGFloat { screenWidth / defaultWidth }
    
    // MARK: - 屏幕像素比例
    @objc public static var scare: CGFloat {
        if #available(iOS 13.0, *) {
            return windowScene?.screen.scale ?? 1
        }
        return UIScreen.main.scale
    }
    // 根据屏幕像素比例，获取横线高度
    @objc public static var lineHeight: CGFloat { CGFloat(scare >= 1 ? 1 / scare : 1) }

    // 状态栏高度
    @objc public static var statusBarHeight: CGFloat {
        if #available(iOS 13.0, *) {
            return keyWindow?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
        }
        return UIApplication.shared.statusBarFrame.height
    }
    // navigationBar 导航栏Bar高度，一般为44
    @objc public static var navigationBarHeight: CGFloat { UINavigationController().navigationBar.frame.size.height }
    // 导航栏高度：状态栏 + navigationBar高度，非刘海屏64，刘海屏 >= 88
    @objc public static var navigationHeight: CGFloat { navigationBarHeight + statusBarHeight }

    // 底部Home条的高度：刘海屏：34，非刘海屏：0
    @objc public static var bottomSafeHeight: CGFloat { keyWindow?.safeAreaInsets.bottom ?? 0 }
    // 动态获取TabBar的高度
    @objc public static var tabBarHeight: CGFloat { UITabBarController().tabBar.frame.size.height + bottomSafeHeight }
}

extension JZDefine {
    // 获取App信息
    @objc public static var infoDict: [String : Any]? { Bundle.main.infoDictionary }
    
    // 获取app的版本号
    @objc public static var appVersion: String { (infoDict?["CFBundleShortVersionString"] as? String) ?? "" }
    
    // 获取app的名称
    @objc public static var appName: String { (infoDict?["CFBundleDisplayName"] as? String) ?? "" }
    
    // 获取app的BundleId
    @objc public static var appBundleId: String { Bundle.main.bundleIdentifier ?? "" }
    
    // 获取设备名称，这个是用户自己定义的
    @objc public static var deviceName: String = UIDevice.current.name
    // 系统名称
    @objc public static var systemName: String = UIDevice.current.systemName
    // 系统版本
    @objc public static var systemVersion: String = UIDevice.current.systemVersion
    
    // 判断是哪种设备
    @objc  public static var interfaceIdiom: UIUserInterfaceIdiom = UIDevice.current.userInterfaceIdiom
    @objc public static func onOS(_ idiom: UIUserInterfaceIdiom) -> Bool {
        if idiom == interfaceIdiom {
            return true
        }
        return false
    }
}


